package com.chebao.util;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;

import com.chebao.exception.GpsException;


public class GpsUtils {
	
	public static Point parseGps(String coordinate) throws GpsException
	{
		if(StringUtils.isNotBlank(coordinate) && !coordinate.equals("00000000000000000000"))
		{
			String prevLng="";
            String prevLat="";
            if(coordinate.substring(0, 1).equals("1")){
                prevLng=getX1(coordinate);   
            }else{
                prevLng=getX2(coordinate);
            }
            prevLat=getY(coordinate);
            
            if (StringUtils.isBlank(prevLng)
            		|| StringUtils.isBlank(prevLat))
            {
            	throw new GpsException("parse longitude or latitude is empty from coordinate=" + coordinate);
            }
            
            double longitude = NumberUtils.toDouble(prevLng);
            double latitude = NumberUtils.toDouble(prevLat);
            Point point = CoordinateConversion.WGS_TO_BD(latitude, longitude);
            return point;
		}
		else
		{
			throw new GpsException("coordinate can not be null、empty and 00000000000000000000");
		}
	}
	
	public static String getX1(String gpsXY){  
        double d1=Double.parseDouble(gpsXY.substring(0,3));
        double d2=Double.parseDouble(gpsXY.substring(3,5)+"."+gpsXY.substring(5,10));
        double d3=Arith.div(d2, 60d, 12);
        return String.valueOf((d1+d3));
    }
    public static String getX2(String gpsXY){
        double d1=Double.parseDouble(gpsXY.substring(0,2));
        double d2=Double.parseDouble(gpsXY.substring(2,4)+"."+gpsXY.substring(4,10));
        double d3=Arith.div(d2, 60d, 12);
        return String.valueOf((d1+d3));
    }
    public static String getY(String gpsXY){
        double d1=Double.parseDouble(gpsXY.substring(10,12));
        double d2=Double.parseDouble(gpsXY.substring(12,14)+"."+gpsXY.substring(14,19));
        double d3=Arith.div(d2, 60d, 12);
        return String.valueOf((d1+d3));
    }
}
